package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.UserMessage;
import com.neusoft.ecommerce.service.UserMessageService;

/**
 * 
 * @author YinKuenTam
 *
 */
@RestController
public class UserMessageController {
	private final UserMessageService userMessageService;

	public UserMessageController(UserMessageService userMessageService) {
		this.userMessageService = userMessageService;
	}
	
	@GetMapping("/userMessage_page")
	public ModelAndView userMessagePage() {
		return new ModelAndView("user/userMessage-list");
	}

	@RequestMapping("/userMessage/list")
	@ResponseBody
	public LayuiPager<UserMessage> list(UserMessage userMessage,
										@RequestParam(defaultValue = "1",value = "page") Integer page,
										@RequestParam(defaultValue = "10",value = "limit") Integer limit) {
		LayuiPager<UserMessage> pager = new LayuiPager<>(page,limit);
		pager.setCondition(userMessage);
		List<UserMessage> list = userMessageService.findList(pager);
		int count = userMessageService.findTotal(pager);
		pager.setData(list);
		pager.setCount(count);

		pager.setCode(0);
		pager.setMsg("我是消息");

		return pager;
	}

	@GetMapping("/addUserMessage")
	public ModelAndView addUserMessagePage() {
		return new ModelAndView("user/userMessage-add");
	}
	
	
	
	@PostMapping("/userMessage/add")
	public Map<String, Object> add(UserMessage userMessage) {
		System.out.println(userMessage);
		Map<String, Object> map = new HashMap<>(16);
		if (userMessageService.add(userMessage)) {
			map.put("msg","ok");
            map.put("code",0);
		} else {
			map.put("msg","失败");
            map.put("code",1);
		}
		return map;
	}
	@GetMapping("/userMessage/edit_page")
	public ModelAndView editPage(UserMessage userMessage, Model model) {
		LayuiPager<UserMessage> pager = new LayuiPager<>(1,1);
        pager.setCondition(userMessage);
        UserMessage message = userMessageService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("user/userMessage-edit");
        model.addAttribute("userMessage", message);
        return view;
	}
	
	
	@PostMapping("/userMessage/edit")
	public Map<String, Object> edit(UserMessage userMessage) {
		System.out.println(userMessage);
		Map<String, Object> map = new HashMap<>(16);
		if (userMessageService.edit(userMessage)) {
			map.put("msg","ok");
            map.put("code",0);
		} else {
			map.put("msg","失败");
            map.put("code",1);
		}
		return map;
	}
	
	
	
	@PostMapping("/userMessage/delete")
	public Map<String, Object> delete(UserMessage userMessage) {
		Map<String, Object> map = new HashMap<>(16);
		if (userMessageService.delete(userMessage)) {
			map.put("msg","ok");
            map.put("code",0);
		} else {
			map.put("msg","失败");
            map.put("code",1);
		}
		return map;
		
	}
}

